package smecta.client.print.controller;

import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import smecta.client.print.common.constants.LoginConstant;
import smecta.client.print.domain.SysUser;
import smecta.client.print.mapper.SysUserMapper;
import smecta.client.print.service.cache.biz.LoginInfoCacheService;
import smecta.client.print.util.AlertUtil;

import java.util.List;

@Component
public class LoginController {

    @Autowired
    private SysUserMapper sysUserMapper;

    @FXML
    private TextField usernameField;
    @FXML
    private TextField passwordField;
    @FXML
    private Button loginButton;
    @FXML
    private Button loginOutButton;

    @FXML
    private VBox vbox;

    @FXML
    private void initialize() {
        Image image1 = new Image("icon/check.png");
        ImageView imageView1 = new ImageView(image1);
        imageView1.setFitWidth(15); // 设置图标的宽度
        imageView1.setFitHeight(12); // 设置图标的高度
        loginButton.setGraphic(imageView1);

        Image image2 = new Image("icon/cancel.png");
        ImageView imageView2 = new ImageView(image2);
        imageView2.setFitWidth(15); // 设置图标的宽度
        imageView2.setFitHeight(12); // 设置图标的高度
        loginOutButton.setGraphic(imageView2);

        // 添加passwordField的回车键监听动作
        passwordField.setOnKeyPressed(event -> {
            if (event.getCode().equals(KeyCode.ENTER)) {
                handleLogin();
            }
        });

        vbox.getStylesheets().add(getClass().getResource("/css/system-login-background-image.css").toExternalForm());
    }

    @FXML
    private void handleLogin() {
        String username = usernameField.getText();
        String password = passwordField.getText();

        if (authenticate(username, password)) {
            Stage stage = (Stage) usernameField.getScene().getWindow();
            stage.close();
            loadMainView();
        } else {
            AlertUtil.error("用户名或者密码错误!");
        }
    }

    @FXML
    private void handleCancel() {
        Stage stage = (Stage) usernameField.getScene().getWindow();
        stage.close();
    }


    private boolean authenticate(String username, String password) {
        SysUser sysUser = new SysUser();
        sysUser.setUserName(username);
        sysUser.setPassword(password);
        List<SysUser> users = sysUserMapper.selectSysUserList(sysUser);
        if(CollectionUtils.isNotEmpty(users)){
            return users.stream().anyMatch(item->{
                LoginInfoCacheService.instance.put(LoginConstant.USER_NAME_CACHE_KEY,item.getUserName());
                return item.getUserName().equals(username) && item.getPassword().equals(password);
            });
        }
        return false;
    }

    private void loadMainView() {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/MainView.fxml"));
            Stage stage = new Stage();
            Scene scene = new Scene(loader.load());
            stage.setScene(scene);
            stage.setTitle("亿维通包装线单机管理软件˾");
            stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                @Override
                public void handle(WindowEvent event) {
                    System.exit(0);
                }
            });
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
